

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
x:serialize(text {
let $doc := $arguments[1]
let $items := doc($doc)/display/item
let $speakers := fn:distinct-values(for $item at $pos in $items return xs:string(jj:get-info($item,"speaker")))
let $code1s := fn:distinct-values(for $item at $pos in $items return xs:string(jj:get-info($item,"code1")))
let $code2s := fn:distinct-values(for $item at $pos in $items return xs:string(jj:get-info($item,"code2")))
let $lines := 
	for $item in $items[info[@name="speaker"]/text()!="NOUVEAU"]
	let $topicnumber := 1+fn:count($item/preceding-sibling::item[info[@name="speaker"]/text()="NOUVEAU"])
	let $time := $item/info[@name="time"]/time/date[1]
	let $speaker := $item/info[@name="speaker"]/text()
	let $code1 := $item/info[@name="code1"]/text()
	let $code2 := $item/info[@name="code2"]/text()
	return (
	fn:concat($topicnumber," ",
		$time," ",
		string(fn:index-of($speakers,$speaker)+100)),
	fn:concat($topicnumber," ",
		$time," ",
		string(fn:index-of($code1s,$code1)+200)),
	fn:concat($topicnumber," ",
		$time," ",
		string(fn:index-of($code2s,$code2)+300))
	)
return fn:string-join($lines,"
")
}, <options method="TEXT"/>)
